// pages/comments/comments.js
let app = getApp();
import api from '../../utils/api.js'
import { HTTP } from '../../utils/http.js'
import { config } from '../../utils/config'
let http = new HTTP()

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		imgUrl: config.imgUrl,
		evalList: [],
		type: 0,
		page: 1,
		isfind: false, //是否查找过 缺省状态判断
		src1: config.imgUrl + '/images/index/xing1.png',
		src2: config.imgUrl + '/images/index/xing2.png',
		typeItem: [
			{ type: 0, typeName: '全部' },
			{ type: 2, typeName: '好评' },
			{ type: 3, typeName: '中评' },
			{ type: 4, typeName: '差评' }
		]
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		let shop_id = options.id;
		if (shop_id) {
			this.setData({
				shop_id
			})
			this.getEvaluateList()
		}

	},

	getEvaluateList() {
		let that = this;
		wx.showLoading({ title: '加载中' })
		http.request({
			url: api.orderEvaluateList,
			method: 'GET',
			data: {
				shop_id: that.data.shop_id,
				type: that.data.type,
				page: that.data.page
			},
			success: (res) => {
				if (res.code == 200) {
					let result = res.data;
					let vegeAll = '';
					result.data.forEach(item => {
						item.user.nickname.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, "");
						if (item.user.nickname.length > 2) {
							item.user.nickname = item.user.nickname.replace(/^(.).*(.)$/, "$1***$2")
						} else {
							item.user.nickname = item.user.nickname.replace(/^(.).*$/, "$1***");
						}
						item.product.forEach(k => {
							vegeAll += k.name + k.count + '份，';
						})
						item.vegeAll = vegeAll.substr(0, vegeAll.length - 1);
					})
					that.setData({
						total: result.total,
						evalList: that.data.evalList.concat(result.data),
						totalPage: result.last_page,
						isfind: true
					})
					wx.hideLoading();

				}
			},
			fail: (res) => {
				wx.showToast({
					title: res.msg,
					icon: 'none',
					duration: 1500
				});
			},
			complete: () => {

			}
		})
	},

	// 预览图片
	previewImg1: function (e) {
		//获取当前图片的下标
		console.log(e)
		let picIndex = e.currentTarget.dataset.picindex;
		let id = e.currentTarget.dataset.id;
		//所有图片
		let evalList = this.data.evalList;
		let current = evalList.find(item => {
			return item.id == id
		})
		wx.previewImage({
			//当前显示图片
			current: current.imgs[picIndex],
			//所有图片
			urls: current.imgs
		})
	},
	changeType(e) {
		let type = e.currentTarget.dataset.type;
		let typeItem = this.data.typeItem;
		let typeObj = typeItem.find(item => {
			return item.type == type
		})
		wx.setNavigationBarTitle({
			title: typeObj.typeName,
		})
		this.setData({
			type: type,
			page: 1,
			evalList: [],
			isfind: false
		})
		this.getEvaluateList();
	},
	onReachBottom: function () {
		console.log('触底刷新');
		if (this.data.page < this.data.totalPage) {
			this.setData({
				page: this.data.page + 1
			})
			this.getEvaluateList();
		}
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {
		let pages = getCurrentPages();// 当前页的数据，
		let prevPage = pages[pages.length - 2];// 上一页的数据，
		prevPage.setData({//对A页面data中的参数进行赋值（记住A页面赋值是在B页面完成的）
			jump: true
		})
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})

function formatName(name) {
	let newStr;
	if (name.length === 2) {
		newStr = name.substr(0, 1) + '*';
	} else if (name.length > 2) {
		let char = '';
		for (let i = 0, len = name.length - 2; i < len; i++) {
			char += '*';
		}
		newStr = name.substr(0, 1) + char + name.substr(-1, 1);
	} else {
		newStr = name;
	}

	return newStr;
}